package com.treasure.service.third.utils;

import com.alibaba.fastjson.JSON;
import com.treasure.service.result.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 *
* @ClassName: RestClient
* @Description: TODO(封装http接口调用)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年4月22日 下午12:40:16
*
 */
@Component
public class RestClient {
	  private static Logger logger = LoggerFactory.getLogger(RestClient.class);
	  @Autowired
	  private RestTemplate restTemplate;
//
	  public <T> T post(String url, Map<String, Object> param, Class<T> responseType) throws HttpServerErrorException {
		  logger.info("调用地址：{},参数{}",url,JSON.toJSONString(param));
		  MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
		  param.keySet().stream().forEach(key -> requestEntity.add(key, param.get(key)));
		  return restTemplate.postForObject(url, requestEntity, responseType);

	  }

	  public <T> T get(String url,Map<String,Object> param,Class<T> responseType)  throws HttpServerErrorException {
		  logger.info("调用地址：{},参数{}",url,JSON.toJSONString(param));
	    return restTemplate.getForObject(url,responseType,param);
	  }
	  public <T> T post(String url, Object json, Class<T> responseType) throws HttpServerErrorException {
		  logger.info("调用地址：{},参数{}",url,JSON.toJSONString(json));
		  HttpHeaders headers = new HttpHeaders();
	      MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
	      headers.setContentType(type);
	      headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//	      HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
	      return restTemplate.postForObject(url, json,responseType);
	  }
	  public <T> T postParams(String url, Object json, Class<T> responseType) throws HttpServerErrorException {
		  logger.info("调用地址：{},参数{}",url,JSON.toJSONString(json));
		  HttpHeaders headers = new HttpHeaders();
		  MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
		  headers.setContentType(type);
		  headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//	      HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
		  return restTemplate.postForObject(url, json,responseType);
	  }

	  public <T> T postList(String url, MultiValueMap<String, Object> map, Class<T> t){
		logger.info("调用地址：{},参数{}",url,JSON.toJSONString(map));
//		MultiValueMap<String, Object> map = new LinkedMultiValueMap();
//		MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//		param.keySet().stream().forEach(key -> map.add(key, String.valueOf(param.get(key))));
		HttpHeaders httpHeaders = new HttpHeaders();
		HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
		ParameterizedTypeReference<PageResult<List<Class<T>>>> pt = new ParameterizedTypeReference<PageResult<List<Class<T>>>>() {
		};

		ResponseEntity<PageResult<List<Class<T>>>> resp = restTemplate.exchange(url,HttpMethod.POST, formEntity, pt);
		/*ResponseEntity<PagedResult<List<TUserPlanDto>>> resp = restTemplate.exchange("http://127.0.0.1:10120" + "/plan/getUserPlanList",
			HttpMethod.POST, formEntity, pt);*/
		return (T) resp.getBody();


	  }

}
